package wat.serwerapp.admins;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import wat.ziszcommon.invoker.Method;
import wat.serwerapp.usermgmt.ldap.RoleManager;

/**
 *
 * @author P.B. & L.G. & K.K.
 */
public class GetRolesList implements Method {

   private static Set<String> owners;

   public Set<String> getOwners() {
      if (owners == null) {
         owners = new HashSet<String>();
         owners.add("pl.edu.wat.erp.client.administration.UserAdministrationClient");
      }
      return owners;
   }

   // role:rightName-rightPath,rightName-rightPath;role:rightName-rightPath
   public String invoke(String parameters) {
      try {
         String response = "";
         Set<String> roles = RoleManager.getAllRolesNames();
         for (String role : roles) {
            String noCnRole = getNoCnName(role);
            System.out.println("ROLE:" + noCnRole);
            response = response + noCnRole + ":";
            Map<String, String> rights = RoleManager.getRights(RoleManager.roleToLdapFormat(noCnRole));
            Set<String> rightKeys = rights.keySet();
            for (String rightKey : rightKeys) {
               String right = rightKey + "-" + rights.get(rightKey);
               System.out.println("RIGHT:" + right);
               response = response + right + ",";
            }
            if (response.lastIndexOf(",") == response.length() - 1) {
               response = response.substring(0, response.length() - 1);
            }
            response = response + ";";
         }
         if (response.lastIndexOf(";") == response.length() - 1) {
            response = response.substring(0, response.length() - 1);
         }
         System.out.println("RESPONSE:");
         System.out.println(response);
         return response;
      } catch (NamingException ex) {
         Logger.getLogger(GetUsersList.class.getName()).log(Level.SEVERE, null, ex);
      }
      return null;
   }

   private String getNoCnName(String name) {
      return name.substring(3);
   }

   public List invoke(List parameters) {
      throw new UnsupportedOperationException("Not supported yet.");
   }
}
